package com.yc.snacknet_vue.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.yc.snacknet_vue.dao.DBHelper;
import com.yc.snacknet_vue.dao.IOrderInfoDAO;

public class OrderInfoDAOImpl implements IOrderInfoDAO{

	@Override
	public String add(String cnos, String ano, String totalPrice) {
		//sqls 和 params
		List<String> sqls = new ArrayList<String>(); //存储要执行的SQL语句
		List<List<Object>> params = new ArrayList<List<Object>>(); //存储每条SQL语句对应的参数
		DBHelper db = new DBHelper();
		System.out.println( cnos );
//		1、订单表 添加   订单编号 ono 时间戳 + 随机数
//		订单编号  下单时间 收获地址编号  订单总额 			待支付
		String ono = new Date().getTime() + "" + new Random().nextInt(1000);
		String sql1 = "insert into orderinfo values( ?, now(), ?, null, null, 1, ?, 0)";
		List<Object> param1 = new ArrayList<Object>();
		Collections.addAll(param1, ono, ano, totalPrice);
		
//		2、订单详情 添加  订单详情编号  订单编号   商品编号  数量 价格   状态
//		insert into orderiteminfo select 0, ?, cf.gno, num, price, 1 from cartinfo cf, goodsinfo gf where cf.gno = gf.gno and cno in (?,?....)
		String sql2 = "insert into orderiteminfo select 0, ?, cf.gno, num, price, 1 from cartinfo cf, goodsinfo gf "
				+ "where cf.gno = gf.gno and cno in ( ";
		List<Object> param2 = new ArrayList<Object>();
		String[] arrs = cnos.split(",");
		param2.add(ono); //存入订单编号
		
//		3、商品表  库存	 修改   	购物买数量  商品编号购物买数量  商品编号购物买数量  商品编号
		String sql3 = "update goodsinfo set balance = balance-case ";  // case when then 条件
		List<Object> param3 = new ArrayList<Object>();
		
		for(String cno: arrs) {		//购物车编号数组
			sql2 += "?, ";
			param2.add( cno );
			
			sql3 += " when gno = (select gno from cartinfo where cno = ?) then  (select num from cartinfo where cno = ?)";
			
			param3.add( cno );
			param3.add( cno );
		}
		sql3 += " end where gno in (select gno from cartinfo where cno in( ";
		
//		4、购物车表  删除  	购物车编号   删除 cnos
		String sql4 = "delete from cartinfo where cno in ( ";
		List<Object> param4 = new ArrayList<Object>();
		
		for(String cno : arrs) {
			sql3 += "?, ";
			param3.add( cno );
			
			sql4 += "?, ";
			param4.add( cno );
		}
		
		sql2 = sql2.substring(0, sql2.lastIndexOf(",")) + ")";
		sql3 = sql3.substring(0, sql3.lastIndexOf(",")) + "))"; //千万不能漏掉  ) 截取最后一个的分割逗号
		sql4 = sql4.substring(0, sql4.lastIndexOf(",")) + ")";
		//存入所有的SQL语句和参数
		Collections.addAll(sqls, sql1, sql2, sql3, sql4);
		Collections.addAll(params, param1, param2, param3, param4);
		
		//执行SQL语句
		try {
			if(db.update(sqls, params) > 0) {  //下单操作 成功
				return ono;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	 	return "";
	}

	@Override
	public List<Map<String, Object>> findByMno(String mno) {
		DBHelper db = new DBHelper();
		String sql = "select o.ono, date_format(odate, '%Y-%m-%d %H:%i') odate, o.status, o.price amount, i.gno, i.nums, i.price, gname,  "
				+ " if(instr(pics, ';') <= 0, pics, left(pics, instr(pics, ';') - 1)) pics, weight, unit from orderinfo o, orderiteminfo i, " 
				+ " goodsinfo g, addrinfo a where o.ono=i.ono and i.gno=g.gno and o.ano=a.ano and a.mno= ? order by o.ono desc";
		return db.findMultiple(sql, mno);
		
	}

	@Override
	public int update(String ono, Integer status) {
		DBHelper db = new DBHelper();
		String sql = "";
		if(status == 3) {  //发货
			sql = "update orderinfo set status = ? and sdate = now() where ono = ?";
		}else if( status == 4) { //收获
			sql = "update orderinfo set status = ? and rdate = now() where ono = ?";
		}else {
			sql ="update orderinfo set status = ? where ono = ?";
		}
		return db.update(sql, status, ono);
	}

	@Override
	public int delete(String ono) {
		DBHelper db = new DBHelper();
		String sql = "delete from orderinfo where ono = ? ";
		return db.update(sql, ono);
	}

}
